package com.dankegongyu.lib.map.baidu;

import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;

import java.lang.ref.WeakReference;

import androidx.annotation.Nullable;

/**
 * 解决OnGetPoiSearchResultListener内存泄漏问题
 *
 * @author wupuquan
 * @date 2020/6/24 11:18
 */
public class DKOnGetPoiSearchResultListener implements OnGetPoiSearchResultListener {

    public interface Proxy {
        void onPoiResult(PoiResult poiResult);

        void onPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult);

        void onPoiIndoorResult(PoiIndoorResult poiIndoorResult);
    }

    private WeakReference<Proxy> mWeakReference;

    public DKOnGetPoiSearchResultListener(Proxy proxy) {
        mWeakReference = new WeakReference<>(proxy);
    }

    @Nullable
    private Proxy getProxy() {
        return mWeakReference.get();
    }

    @Override
    public final void onGetPoiResult(PoiResult poiResult) {
        Proxy proxy = getProxy();
        if (proxy != null) {
            proxy.onPoiResult(poiResult);
        }
    }

    @Override
    public final void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

    }

    @Override
    public final void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {
        Proxy proxy = getProxy();
        if (proxy != null) {
            proxy.onPoiDetailResult(poiDetailSearchResult);
        }
    }

    @Override
    public final void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
        Proxy proxy = getProxy();
        if (proxy != null) {
            proxy.onPoiIndoorResult(poiIndoorResult);
        }
    }

}
